$(function () {
  $("form").on("submit", function (event) {
    event.preventDefault();
    var username = $(".username").val();
    var password = $(".password").val();
    var rpassword = $(".rpassword").val();
    var nickname = $(".nickname").val();
    console.log(password, rpassword);

    if (!username || !password || !rpassword || !nickname) {
      alert("必填项不能为空！");
      return false;
    }

    if (password !== rpassword) {
      alert("密码不一致！");
      return false;
    }

    axios
      .post("http://localhost:9000/users/register", {
        username,
        password,
        rpassword,
        nickname,
      })
      .then(function (response) {
        if (response.data.code === 1) {
          alert(response.data.message);
          window.location.href = "./login.html";
        } else {
          alert(response.data.message);
        }
      })
      .catch(function (error) {
        console.error("error", error);
      });
  });
});
